package com.tongzhou.tongzhou.quot.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.quot.dao.IQuotUrlDAO;
import com.tongzhou.tongzhou.quot.model.QuotUrl;
import com.tongzhou.tongzhou.quot.model.SearchQuotUrlCondition;
import com.tongzhou.util.FileUtil;

public class QuotUrlAction extends ActionSupport{
	private int id;
	private QuotUrl quotUrl;
	private List<QuotUrl> result=null;
	private SearchQuotUrlCondition condition=null;
	
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	
	private IQuotUrlDAO dao=null;
	
	private File url ;
	private String urlFileName ;
	/**
	 * 报价附件列表界面
	 *@author: 彭定雄
	 *@return 
	 *@throws DBException
	 */
	public String listQuotUrl() throws DBException{
		checkSearchCondition();
		condition = new SearchQuotUrlCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setCurrentPage(currentPage) ;
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType) ;
		condition.setPageSize(pageSize) ;
	    result = dao.listQuotUrl(condition);
		return "listQuotUrl";
	} 
	
	
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "desc" ;
		}
	}
	
	/**
	 * 新增报价附件
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toAddQuotUrl() throws DBException{
		return "addQuotUrl";
	}
	
	public String addQuotUrl() throws DBException{
		if(url!=null){
			String s=urlFileName.replace(".", "`").split("`")[1];
			String path = "quot"+File.separator+FileUtil.generateFileName()+"."+s;
			path=path.replace("\\","\\\\");
			FileUtil.copyFile(url, new File(FileUtil.getWebRootDir()+path));
			quotUrl.setUrl(path);
		}
		dao.addQuotUrl(quotUrl);
		listQuotUrl();
		return "listQuotUrl";
	}
	
	public String deleteQuotUrl() throws DBException{
//		dao=new UserMgrDAO();
		dao.deleteQuotUrl(id);
		listQuotUrl();
		return "listQuotUrl";
	}
	
	public String toEditQuotUrl() throws DBException{
		quotUrl=dao.getQuotUrlById(id);
		return "editQuotUrl";
	}
	
	public String editQuotUrl() throws DBException{
		if(url!=null){
			String s=urlFileName.replace(".", "`").split("`")[1];
			String path = "quot"+File.separator+FileUtil.generateFileName()+"."+s;
			path=path.replace("\\","\\\\");
			FileUtil.copyFile(url, new File(FileUtil.getWebRootDir()+path));
			quotUrl.setUrl(path);
		}else{
			quotUrl.setUrl(quotUrl.getUrl().replace("\\","\\\\"));
		}
		dao.updateQuotUrl(quotUrl);
		listQuotUrl();
		return "listQuotUrl";
	}
	
	/**
	 * 下载文件
	 */
	public String downLoadFile(){
		HttpServletResponse response = ServletActionContext.getResponse();
		
//		PrintWriter out = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			quotUrl=dao.getQuotUrlById(id);
			String filedisplay ="";
			int len=0;
			if(quotUrl.getUrl().contains("\\")){
				len=quotUrl.getUrl().replace("\\", "`").split("`").length;
				filedisplay =quotUrl.getUrl().replace("\\", "`").split("`")[len-1]; 
			}else{
				len=quotUrl.getUrl().replace("/", "`").split("`").length;
				filedisplay =quotUrl.getUrl().replace("/", "`").split("`")[len-1]; 
			}
			response.setContentType("application/x-msdownload;");
			response.setHeader("Content-disposition", "attachment; filename="
			  + new String(filedisplay.getBytes("gb2312"), "ISO8859-1"));
			response.setHeader("Content-Length", String.valueOf(filedisplay));
			response.setCharacterEncoding("utf-8");
			bis = new BufferedInputStream(new FileInputStream(quotUrl.getUrl()));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
	  		if (bis != null){
	  			try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	  		}
	  		if (bos != null){
	  			try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	  		}
	  	}
		return null;
	}

	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public QuotUrl getQuotUrl() {
		return quotUrl;
	}


	public void setQuotUrl(QuotUrl quotUrl) {
		this.quotUrl = quotUrl;
	}


	public File getUrl() {
		return url;
	}


	public void setUrl(File url) {
		this.url = url;
	}


	public String getUrlFileName() {
		return urlFileName;
	}


	public void setUrlFileName(String urlFileName) {
		this.urlFileName = urlFileName;
	}


	public List<QuotUrl> getResult() {
		return result;
	}


	public void setResult(List<QuotUrl> result) {
		this.result = result;
	}


	public SearchQuotUrlCondition getCondition() {
		return condition;
	}


	public void setCondition(SearchQuotUrlCondition condition) {
		this.condition = condition;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public String getOrderBy() {
		return orderBy;
	}


	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}


	public String getOrderByType() {
		return orderByType;
	}


	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}


	public IQuotUrlDAO getDao() {
		return dao;
	}


	public void setDao(IQuotUrlDAO dao) {
		this.dao = dao;
	}

}
